layui.use(['form', 'jquery', 'layer', 'upload'], function () {
    let $ = layui.jquery,
        layer = layui.layer,
        upload = layui.upload,
        form = layui.form;
    if (!localStorage.getItem('Authorization')) {
        layer.alert('请先登录', {
            skin: 'layui-layer-molv' //样式类名  自定义样式
            , closeBtn: 0    // 是否显示关闭按钮
            , anim: 1 //动画类型
            , btn: ['返回主页'] //按钮
            , icon: 6    // icon
            , yes: function () {
                location.href = '/'
            }
        });
    }
    form.verify({
        pass: [/(.+){6,12}$/, '密码必须6到12位'],
        repass: function (value) {
            if ($('#L_pass1').val() != $('#L_repass2').val()) {
                return '两次密码不一致';
            }
        }
    });

    $(function () {
        setTimeout(function () {
            frameVal();
        }, 100);
    });


    function frameVal() {
        $.ajax({
            type: "GET",
            url: '/api/user/getOneMember',
            dataType: 'json',
            data: {
                id: localStorage.getItem('id')
            },
            success: function (data) {
                if (data.status === 200) {
                    let username = data.data.username; //用户名
                    $('#L_username').val(username);
                    $('#name').val(data.data.name)
                    console.log(data.data.sex)
                    if (data.data.sex === true) {
                        data.data.sex = 1
                    } else {
                        data.data.sex = 0
                    }
                    $('input[name="sex"][value="' + data.data.sex + '"]').attr("checked", true);
                    $('#tel').val(data.data.tel)
                    $('#email').val(data.data.email)
                    form.render();
                } else {
                    layer.msg(data.msg)
                }
            },
            err: function () {
                layui.use('layer', function () {
                    layer.msg("服务器请求失败")
                });
            }
        });
    }

    $('#profile-photo').attr('src', localStorage.getItem('photo'))
    let uploadInst = upload.render({
        elem: '#photo-upload' //绑定元素
        , url: '/api/article/uploadPhoto' //上传接口
        ,size: 5120
        , done: function (res) {
            //上传完毕回调
            $.ajax({
                type: "POST",
                url: '/api/user/updateInfo',
                dataType: 'json',
                headers: {
                    'Authorization': localStorage.getItem('Authorization')
                },
                data: {
                    photo: res.data.src
                },
                success: function (data) {
                    if (data.status === 200) {
                        localStorage.photo = res.data.src
                        $('#profile-photo').attr('src', res.data.src)
                        layer.msg('头像修改成功')
                    } else {
                        layer.msg(data.msg)
                    }
                },
                err: function () {
                    layui.use('layer', function () {
                        layer.msg("服务器请求失败")
                    });
                }
            });
        }
        , error: function () {
            //请求异常回调
        }
    });

    $('#info-form input').attr('disabled', true);
    $('#info-form input').attr('readonly', true);
    $("input[name='sex']").next().addClass('layui-radio-disbaled layui-disabled');
    $('#edit').click(function () {
        $(this).hide()
        $('#change-password').hide()
        $('#info-form input').attr('disabled', false);
        $('#info-form input').attr('readonly', false);
        $("input[name='sex']").next().removeClass('layui-radio-disbaled layui-disabled');
        $('#info-submit').show()
        $('#back').show()
    })

    $('#change-password').click(function () {
        $('#edit').hide()
        $(this).hide()
        $('#password-form').show()
        $('#info-form').hide()
        $('#back').show()
    })

    form.on('submit(change-info)', function (data) {
        let name, sex, tel, email
        name = data.field.name
        tel = data.field.tel
        email = data.field.email
        if (data.field.sex === "女") {
            sex = 0
        } else {
            sex = 1
        }
        $.ajax({
            type: "POST",
            url: '/api/user/updateInfo',
            dataType: 'json',
            headers: {
                'Authorization': localStorage.getItem('Authorization')
            },
            data: {
                name: name,
                tel: tel,
                sex: sex,
                email: email
            },
            success: function (data) {
                if (data.status === 200) {
                    layer.msg('修改成功')

                    $('#info-form input').attr('disabled', true);
                    $('#info-form input').attr('readonly', true);
                    $("input[name='sex']").next().addClass('layui-radio-disbaled layui-disabled');
                    $('#edit').show()
                    $('#info-submit').hide()
                    $('#change-password').show()
                } else {
                    layer.msg(data.msg)
                }
            },
            err: function () {
                layui.use('layer', function () {
                    layer.msg("服务器请求失败")
                });
            }
        });
        return false;
    });
    form.on('submit(btn-change-password)', function () {
        // layer.msg(data.field)
        let old_password = $('input[name="oldpass"]').val();
        let new_password = $('input[name="newpass"]').val();
        $.ajax({
            type: "POST",
            url: '/api/user/changePassword',
            dataType: 'json',
            data: {
                id: localStorage.getItem('id'),
                old_password: old_password,
                new_password: new_password,
            },
            success: function (data) {
                if (data.status === 200) {
                    layer.msg('修改成功')
                } else {
                    layer.msg(data.msg)
                }
            },
            err: function () {
                layer.msg("服务器请求失败")
            }
        });
        return false;
    })
    $('#back').click(function () {
        $(this).hide()
        $('#change-password').show()
        $('#edit').show()
        $('#info-form input').attr('disabled', true);
        $('#info-form input').attr('readonly', true);
        $('#info-submit').hide()
        $('#password-form').hide()
        $('#info-form').show()
        $('#L_pass').val("")
        $('#L_pass1').val("")
        $('#L_repass2').val("")
    })
});

function getUrlParam(name) {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    let r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]);
    return null; //返回参数值
}